/*
 * P2PClient.java
 *
 * Created on 19 de julio de 2007, 04:33
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ws;

import java.io.ByteArrayOutputStream;
import javax.xml.soap.SOAPMessage;

/**
 *
 * @author mariano
 */
public class P2PClient {
    protected String url;
    protected String serviceName;
    protected String portName;
    protected String nameSpace;
    protected String uri;
    protected soap.Client client;
    
    /** Creates a new instance of P2PClient */
    public P2PClient(String url, String serviceName, String portName, String nameSpace, String uri)
    {
        this.url = url;
        this.serviceName = serviceName;
        this.portName = portName;
        this.nameSpace = nameSpace;
        this.uri = uri;
        
        // parameter example:
        // "http://localhost:8765/", "Fresitaws", "FresitaPort", "http://localhost:8765/", "http://fresita/"
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getPortName() {
        return portName;
    }

    public void setPortName(String portName) {
        this.portName = portName;
    }

    public String getNameSpace() {
        return nameSpace;
    }

    public void setNameSpace(String nameSpace) {
        this.nameSpace = nameSpace;
    }

    public soap.Client getClient() {
        return client;
    }

    public void setClient(soap.Client client) {
        this.client = client;
    }
    
    // cosas interesantes
    
    public boolean ping()
    {
        client = new soap.Client( url, serviceName, portName, nameSpace );
        soap.SoapMessage message = new soap.SoapMessage();
        soap.SoapMethod method = new soap.SoapMethod( "ping", "s", uri );
        
        try
        {
            message.addMethod(method);
            SOAPMessage response = client.invoke(message.toString());
            if( soap.Client.getMessageResponse( response, "return" ).equals("pong") )
            {
                return true;
            }
            else
            {
                response.writeTo( System.err );
                return false;
            }
                
        }
        catch( Exception ex )
        {
            System.err.println( "error" + ex.toString() );
            return false;
        }
    }
    
    public MachineList getMachineList()
    {
        client = new soap.Client( url, serviceName, portName, nameSpace );
        soap.SoapMessage message = new soap.SoapMessage();
        soap.SoapMethod method = new soap.SoapMethod( "getMachineList", "s", uri );
        
        try
        {
            message.addMethod(method);
            SOAPMessage response = client.invoke(message.toString());
            return MachineList.fromXml( soap.Client.getMessageResponse( response, "return" ) );
        }
        catch( Exception ex )
        {
            System.err.println( "error" + ex.toString() );
            return null;
        }
    }
    
}
